package com.bw.module_home.widget;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

import com.bw.module_home.R;

/**
 * @Author : ZhouZiYao
 * @Time : On 2023/11/25 08:45
 * @Description :
 */
public class RedPacketView extends View {

    private Paint bgPaint;//背景圆环画笔
    private Paint fgPaint;//前景圆环画笔
    private Paint bitmapPaint;//图片画笔
    int storkeWidth = 10;//圆环粗细
    /**
     * 默认宽高
     */
    private int defaultWidth=150;
    private int defaultHeight=150;

    private Callback callback=null;//回调接口
    /**
     * 扫描弧度
     */
    private int angle=0;
    /**
     * 弧形范围
     */
    private RectF rectF=null;
    private Bitmap bitmap;
    /**
     * 图片范围
     */
    private Rect rect;
    private Rect dst;


    private ValueAnimator valueAnimator;//动画


    public RedPacketView(Context context) {
        super(context);
        init();
    }

    public RedPacketView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public RedPacketView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    public RedPacketView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init();
    }

    private void init() {
        initPaint();
    }
    /**
     * 开始动画
     */
    public void startAnimate(){
        valueAnimator = ValueAnimator.ofFloat(0,1);
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                float value= (float) animation.getAnimatedValue();//小数点
                angle= (int) (360*value);

                invalidate();//重新绘制
            }
        });
        valueAnimator.addListener(new AnimatorListenerAdapter() {

            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                if (null!=callback){
                    angle=0;
                    invalidate();
                    callback.completed();
                }
            }

            @Override
            public void onAnimationStart(Animator animation) {
                super.onAnimationStart(animation);
                if (null!=callback){
                    callback.start();
                }
            }

            @Override
            public void onAnimationPause(Animator animation) {
                super.onAnimationPause(animation);
                if (null!=callback){
                    callback.pause(angle);
                }
            }

            @Override
            public void onAnimationResume(Animator animation) {
                super.onAnimationResume(animation);
                if (null!=callback){
                    callback.resume();
                }
            }
        });
        valueAnimator.setDuration(10000);
        valueAnimator.start();

    }

    /**
     * 暂停动画
     */
    public void pause(){
        if (null!=valueAnimator){
            valueAnimator.pause();
        }
    }

    /**
     * 恢复动画
     */
    public void resume(){
        if (null!=valueAnimator){
            valueAnimator.resume();
        }
    }

    /**
     * 停止动画
     */
    public void stop(){
        if (null!=valueAnimator){
            valueAnimator.cancel();
        }
    }

    /**
     * 初始化画笔方法
     */
    private void initPaint() {
        bgPaint=new Paint();
        bgPaint.setStyle(Paint.Style.STROKE);//空心
        bgPaint.setColor(Color.rgb(221,221,221));//灰色
        bgPaint.setStrokeWidth(storkeWidth);//粗细

        fgPaint=new Paint();
        fgPaint.setStyle(Paint.Style.STROKE);
        fgPaint.setColor(Color.rgb(255,0,51));//红色
        fgPaint.setStrokeWidth(storkeWidth);

        bitmapPaint=new Paint();
        bitmapPaint.setAntiAlias(true);
        bitmapPaint.setDither(true);
    }
    int centerX=0;//圆心坐标
    int centerY=0;//圆心坐标
    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);

        bitmap=getBitmap(R.drawable.redpacket,getMeasuredWidth()/2,getMeasuredHeight()/2);

        int padding=storkeWidth+2;
        //圆弧的区域
        rectF=new RectF(0+padding,0+padding,getMeasuredWidth()-padding,getMeasuredHeight()-padding);
        //中心点
        centerX=getMeasuredWidth()/2;
        centerY=getMeasuredHeight()/2;
        //图片范围
        rect = new Rect(0,0,bitmap.getWidth(),bitmap.getHeight());
        //目标图片范围
        dst = new Rect(centerX-bitmap.getWidth()/2,centerY-bitmap.getHeight()/2,centerX+bitmap.getWidth()/2,centerY+getHeight()/2);
    }

    /**
     * Bitmap二次采样/尺寸压缩 重点！！后面解释
     * @param id
     * @param w
     * @param h
     * @return
     */
    private Bitmap getBitmap(int id,int w,int h) {
        BitmapFactory.Options options=new BitmapFactory.Options();
        options.inJustDecodeBounds=true;
        BitmapFactory.decodeResource(getResources(),id,options);
        int oldWidth=options.outWidth;
        int oldHeight=options.outHeight;
//        int min = Math.min(oldWidth, oldHeight);

        int sampleSize = 1;
        while (oldHeight / sampleSize > h || oldWidth / sampleSize > w) {
            sampleSize *= 2;
        }
        options.inJustDecodeBounds=false;
        options.inSampleSize=sampleSize;
        options.inPreferredConfig= Bitmap.Config.RGB_565;

        Bitmap result= BitmapFactory.decodeResource(getResources(),R.drawable.redpacket,options);
        return result;
    }


    /**
     * 测量，很重要，后面解释
     * @param widthMeasureSpec
     * @param heightMeasureSpec
     */
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        //宽度测量模式
        int widthMode=MeasureSpec.getMode(widthMeasureSpec);
        //宽度测量大小
        int widthSize=MeasureSpec.getSize(widthMeasureSpec);
        //高度测量模式
        int heightMode=MeasureSpec.getMode(heightMeasureSpec);
        //高度测量大小
        int heightSize=MeasureSpec.getSize(heightMeasureSpec);

        if (widthMode==MeasureSpec.AT_MOST&&heightMode==MeasureSpec.AT_MOST){//wrap_content
            setMeasuredDimension(defaultWidth,defaultHeight);
        }else if (widthMode==MeasureSpec.AT_MOST){
            setMeasuredDimension(defaultWidth,heightSize);
        }else if (heightMode==MeasureSpec.AT_MOST){
            setMeasuredDimension(widthSize,defaultHeight);
        }
    }

    /**
     * 绘制view
     * @param canvas
     */
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawBitmap(bitmap, rect, dst,bitmapPaint);//图片
        canvas.drawArc(rectF,0,360,false,bgPaint);//背景圆弧
        canvas.drawArc(rectF,270,angle,false,fgPaint);//前置圆弧

    }

    /**
     * 设置监听
     * @param callback
     */
    public void setCallback(Callback callback) {
        this.callback = callback;
    }
    public interface Callback{
        void start();
        void pause(int angle);
        void resume();
        void completed();
    }
}
